//
//  photoBrowserAnimator.swift
//  新浪微博01
//
//  Created by 李国伟 on 15/12/20.
//  Copyright © 2015年 李国伟. All rights reserved.
//

import UIKit

//MARK: 入场动画代理
protocol PhotoBrowserPresentDelegate: NSObjectProtocol{
    
    //返回选定的image
    func presentImage(indexPath : NSIndexPath) -> UIImageView
    
    //返回选定的其实位置
    func presentStartRect(indexPath : NSIndexPath) -> CGRect
    
    //返回最终的位置
    func presentEndRect(indexPath : NSIndexPath) -> CGRect
}

//MARK: 出场动画
protocol PhotoBrowserDismissDelegate: NSObjectProtocol{
    
    //返回大图
    func imageViewForDismiss() -> UIImageView

    
    //返回选定的其实位置
    func indexPathForDismiss() -> NSIndexPath

}

class PhotoBrowserAnimator: NSObject,UIViewControllerTransitioningDelegate {

    private var isPresent = false
    var indexPath : NSIndexPath?
    weak var presentDelegate: PhotoBrowserPresentDelegate?
    weak var dismissDelegaet: PhotoBrowserDismissDelegate?
    
    //设置model代理对象
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresent = true
        return self
    }
    
    //设置dismiss代理对象
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresent = false
        return self
    }
    
    /// 设置代理参数
    ///
    /// - parameter presentDelegate: 展现转场代理
    /// - parameter dismissDelegate: 解除转场代理
    /// - parameter indexPath:       照片索引
    func setDelegateParams(presentDelegate: PhotoBrowserPresentDelegate,
        dismissDelegate: PhotoBrowserDismissDelegate,
        indexPath: NSIndexPath) {
            
            self.presentDelegate = presentDelegate
            self.dismissDelegaet = dismissDelegate
            self.indexPath = indexPath
    }

}

extension PhotoBrowserAnimator: UIViewControllerAnimatedTransitioning{
    
    //设置动画时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    
    /// 转场动画实现方法 － 一旦实现此函数，系统的动画方法，将由程序员负责
    ///
    /// - parameter transitionContext: 转场上下文 - 提供转场动画的所有细节
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        isPresent ? presentAnimation(transitionContext) : dismissAnimation(transitionContext)
    }
    
    //入场动画
    private func presentAnimation(transitionContext: UIViewControllerContextTransitioning){
        
        guard let indexpath = indexPath, presentDelegate = presentDelegate else{
            return
        }
        
        let toview = transitionContext.viewForKey(UITransitionContextToViewKey)
        let toController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) as! PictureController
        transitionContext.containerView()?.addSubview(toview!)
//        toController.collectionView.hidden = true
        toview?.alpha = 0
        
        
        let imageView = presentDelegate.presentImage(indexpath)
        imageView.frame = presentDelegate.presentStartRect(indexpath)
        transitionContext.containerView()?.addSubview(imageView)
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            imageView.frame = presentDelegate.presentEndRect(indexpath)
            imageView.alpha = 0
            toview?.alpha = 1
//            toController.collectionView.hidden = false
            }) { (_) -> Void in
           imageView.removeFromSuperview()
           transitionContext.completeTransition(true)
        }
    }
    
    //出场动画
    private func dismissAnimation(transitionContext: UIViewControllerContextTransitioning){
        guard let dismissDelegaet = dismissDelegaet,presentDelegate = presentDelegate else{
            return
        }
        
      let fromview = transitionContext.viewForKey(UITransitionContextFromViewKey)
      fromview?.removeFromSuperview()
        
      let imageview = dismissDelegaet.imageViewForDismiss()
      transitionContext.containerView()?.addSubview(imageview)
        
      let indexpath = dismissDelegaet.indexPathForDismiss()
      UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
        imageview.alpha = 0
        imageview.frame = presentDelegate.presentStartRect(indexpath)
        }) { (_) -> Void in
            imageview.removeFromSuperview()
            transitionContext.completeTransition(true)
        }
    }
}